
class Admin::ProductImagesController < Admin::BaseController

  before_action :find_product

  def index
    @product_images = @product.images
  end

  def create
    if params[:image].present?
    @product.images << ProductImage.new(image: params[:image])

    redirect_to :back
    else
      redirect_to action: :index
    end
  end

  def destroy
    @product_image = @product.images.find(params[:id])
    if @product_image.destroy
      flash[:notice] = "删除成功"
    else
      flash[:notice] = "删除失败"
    end

    redirect_to :back
  end

  def update
    @product_image = @product.images.find(params[:id])
    @product_image.weight = params[:weight]
    if @product_image.save
      flash[:notice] = "修改成功"
    else
      flash[:notice] = "修改失败"
    end

    redirect_to :back
  end

  private
  def find_product
    @product = Product.unscoped.find params[:product_id]
  end

end

